#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

int main(int argc, char const *argv[])
{
    int ret = -1;
    int fd = -1;
    ret = mkfifo("fifo",0664); //创建有名管道
    if (ret == -1){
        if (errno != 17){ //错误码17代表fifo管道已经存在
            printf("mkfifo failed\n");
            return -1;
        }else {
            printf("fifo is existing\n");
        }
    }

    fd = open("fifo", O_RDWR); //打开有名管道
    if (fd < 0){
        printf("open failed\n");
        return -1;
    }

    while(1){
        write(fd,"hello world!",sizeof("hello world!")); //往管道里面写入数据hello world!
        sleep(2);
    }


    return 0;
}